import equipmentInformationVue from "../pages/equipmentInformation/equipmentInformation.vue";

// 将16进制的内容转成我们看得懂的字符串内容
export function hexCharCodeToStr(hexCharCodeStr) {
	var trimedStr = hexCharCodeStr.trim();
	var rawStr = trimedStr.substr(0, 2).toLowerCase() === "0x" ? trimedStr.substr(2) : trimedStr;
	var len = rawStr.length;
	if (len % 2 !== 0) {
		alert("存在非法字符!");
		return "";
	}
	var curCharCode;
	var resultStr = [];
	for (var i = 0; i < len; i = i + 2) {
		curCharCode = parseInt(rawStr.substr(i, 2), 16);
		resultStr.push(String.fromCharCode(curCharCode));
	}
	return resultStr.join("");
}

// 查询发送二进制数据
export function EnquirieswriteBLECharacteristicValue(data) {
	let characteristics = getApp().globalData.characteristics
	let characteristicId = ""
	for (var i = 0; i < characteristics.length; i++) {
		if (characteristics[i].properties.write == true) {
			characteristicId = characteristics[i].uuid
		}
	}
	let msg = data + "?"
	// 向蓝牙设备发送一个0x00的16进制数据
	const buffer = new ArrayBuffer(msg.length)
	const dataView = new DataView(buffer)
	// dataView.setUint8(0, 0)
	for (var i = 0; i < msg.length; i++) {
		dataView.setUint8(i, msg.charAt(i).charCodeAt())
	}

	console.log('查询要发送的信息是：' + String.fromCharCode.apply(null, new Uint8Array(buffer)))


	setTimeout(() => {
		uni.writeBLECharacteristicValue({
			// 这里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
			deviceId: getApp().globalData.deviceId,
			// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
			serviceId: getApp().globalData.uuid,
			// 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
			characteristicId: characteristicId,
			// 这里的value是ArrayBuffer类型
			// value: buffer,
			value: buffer,
			//writeType: 'write',
			success: (res) => {
				//that.returnMessage = res.errMsg;
				console.log("发送成功-----", res);
			},
			fail: (res) => {
				//that.returnMessage = res.errMsg;
				console.log("发送失败-----", res);
			},
		
			complete: (res) => {
				console.log("发送消息结束----", res)
			}
		});
	},1000)
	
}

// 设置发送二进制数据
export function SetwriteBLECharacteristicValue(data) {
	let characteristics = getApp().globalData.characteristics
	let characteristicId = ""
	for (var i = 0; i < characteristics.length; i++) {
		if (characteristics[i].properties.write == true) {
			characteristicId = characteristics[i].uuid
		}
	}
	let msg = data + ";"
	// 向蓝牙设备发送一个0x00的16进制数据
	const buffer = new ArrayBuffer(msg.length)
	const dataView = new DataView(buffer)
	// dataView.setUint8(0, 0)
	for (var i = 0; i < msg.length; i++) {
		dataView.setUint8(i, msg.charAt(i).charCodeAt())
	}

	console.log('设置要发送的信息是：' + String.fromCharCode.apply(null, new Uint8Array(buffer)))


	uni.writeBLECharacteristicValue({
		// 这里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
		deviceId: getApp().globalData.deviceId,
		// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
		serviceId: getApp().globalData.uuid,
		// 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
		characteristicId: characteristicId,
		// 这里的value是ArrayBuffer类型
		// value: buffer,
		value: buffer,
		writeType: 'write',
		success: (res) => {
			//that.returnMessage = res.errMsg;
			console.log("发送成功-----", res);
		},
		fail: (res) => {
			//that.returnMessage = res.errMsg;
			console.log("发送失败-----", res);
		},

		complete: (res) => {
			console.log("发送消息结束----", res)
		}
	});
}


// ArrayBuffer转16进度字符串示例
export function ab2hex(buffer) {
	const hexArr = Array.prototype.map.call(
		new Uint8Array(buffer),
		function(bit) {
			return ('00' + bit.toString(16)).slice(-2)
		}
	)
	return hexArr.join('')
}

//返回数据截取使用
export function result1(result) {
	//console.log("返回数据截取使用----", result == "",typeof result != "undefined",result == null)
	if(typeof result != "undefined") {
		let split = result.replace(/\s+/g, '').replace(';', '').split(",")
		return split[1]
	}else 
	return ''
}

//返回数据截取使用
export function result2(result) {
	if(typeof result != '') {
		let split = result.replace(/\s+/g, '').replace(';', ',').split(",")
		return split[2]
	}else return ''
	
}

//返回数据截取使用
export function result3(result) {
	if(typeof result != '') {
		let split = result.replace(/\s+/g, '').replace(';', '').split(",")
		return split[3]
	}else return ''
}

//返回数据截取使用
export function result4(result) {
	if(typeof result != '') {
		let split = result.replace(/\s+/g, '').replace(';', ',').split(",")
		return split[4]
	}else return ''
	
}


//开关
export function getOnOff(result) {
	if(typeof result != '') {
		if (result == 0) {
			return 'O'
		} else if (result == 1) {
			return 'C'
		} else if (result == 'O') {
			return '开'
		} else if (result == 'C') {
			return '关'
		} else if (result == 'N') {
			return '正常'
		} else if (result == 'E' || result == 'F') {
			return '异常'
		}
	}else return ''
}

//通信状态
export function getTongXin(result) {
	if(typeof result != '') {
		switch (result) {
			case 'O':
				return '正常'
				break
			case 'E':
				return '异常'
				break
			case 'N':
				return '未知'
				break
			case 'A':
				return '有效'
				break
			case 'V':
				return '无效'
				break
		}
	}else return ''
}

export function getType(result) {
	if(typeof result != '') {
		switch (result) {
			case 'A':
				return '应答'
				break
			case 'N':
				return '无应答'
				break
			case 'M':
				return '调试'
				break
			case 'R':
				return '实时'
				break
			case 'S':
				return '正常'
				break
		}
	}else return ''
}

export function getZhuangtai(result) {
	if(typeof result != '') {
		switch (result) {
			case 'U':
				return '上限'
				break
			case 'D':
				return '下限'
				break
			case 'R':
				return '上升'
				break
			case 'F':
				return '下降'
				break
			case 'H':
				return '静止'
				break
			case 'O':
				return '脱线'
				break
			case 'B':
				return '卡线'
				break
			case 'S':
				return '粘线'
				break
		}
	}else return ''
}

//数组去重
export function unique(arr) {
	for (let i = 0; i < arr.length; i++) {
		// let num=0;
		for (let j = i + 1; j < arr.length; j++) {
			if (arr[i].name == arr[j].name) { 
				if(arr[j].value.indexOf(arr[i].value) != -1) {
					arr.splice(j, 1);
				}
			}
		}
	}
	return arr
}

// // 读取设备二进制数据
// readBLECharacteristicValue() {
// 	// console.log('进入读取');
// 	// setTimeout(()=>{
// 	uni.readBLECharacteristicValue({
// 		// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
// 		deviceId: this.deviceId,
// 		// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
// 		serviceId: this.uuid,
// 		// 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
// 		characteristicId: this.characteristics,
// 		success: (res) => {
// 			console.log("读取成功:", res);
// 		},
// 		fail: (res) => {
// 			console.log("读取失败:", res);
// 		},
// 	});
// },
